package com.test.volatileDome;

import java.util.concurrent.TimeUnit;

/**
 * 1.验证volstile可见性
 *   1.1 假如int num = 0；num变量之前没有volatile关键字修饰
 *
 * @author wangjie
 * @version V1.0
 * @date 2019/12/14
 */
public class VolatileDemo {

    public static void main(String[] args) {
        MyData myData = new MyData();

        //第一个线程
        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "\t come in");
            try {
                //暂停一下线程
                TimeUnit.SECONDS.sleep(5);
                myData.add();
                System.out.println(Thread.currentThread().getName() + "\t updated num value ：" + myData.num);
            }catch (Exception e){
                e.printStackTrace();
            }
        },"AAA").start();

        //main线程
        while (0 == myData.num){

            //main线程就一直在此等待，循环
        }
        System.out.println(Thread.currentThread().getName() + "\t over" + myData.num);
    }
}

class MyData{

    int  num = 0;

    public void add(){
        num = 1;
    }
}
